﻿using DotNetStar.TheKing.Domain;
using DotNetStar.TheKing.Infrastructure;
using DotNetStar.TheKing.Services;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace DotNetStar.TheKing.Web
{
    /// <summary>
    /// 内容平台的首页
    /// </summary>
    public class ContentIndexContentViewComponent : ViewComponent
    {
        private readonly ContentService _contentService;
        public ContentIndexContentViewComponent(ContentService contentService)
        {
            _contentService = contentService;
        }

        public async Task<IViewComponentResult> InvokeAsync(int? categoryId)
        {            
            //所有热门帖子
            ViewData["HotContents"] = _contentService.GetTopContentQueable(ContentQuery.Default.OnlyContentChannel())
                .OrderByDescending(m => m.VisitNum)
                .ToList();

            //当前分类的排行榜
            ViewData["HotRelatedContent"] = _contentService.GetTopContentQueable(new ContentQuery
            {
                CategoryId = categoryId
            })
                .OrderByDescending(m => m.VisitNum)
                .ToList();

            var contents = await _contentService.GetContentsAsync(new ContentPageQuery(Request.Query["page"].ToString().To(1))
            {
                CategoryId = categoryId,
                Channel =  Channels.Content
            });


            return View(contents);
        }
    }
}
